package com.heroCW.bodhiAttic.resultUtil;

import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * 返回信息
 */
@Data
public class Result<T> {

    private Integer code; //编码：1成功，0和其它数字为失败

    private String msg; //错误信息

    private T data; //数据

    private Map<String, Object> map = new HashMap<>(); //动态数据

    public Result(Integer code, T data) {
        this.data = data;
        this.code = code;
    }

    public Result(Integer code, String msg) {
        this.msg = msg;
        this.code = code;
    }

    public Result(Integer code, T data, String msg) {
        this.data = data;
        this.code = code;
        this.msg = msg;
    }

    private final static Integer SUCCESS_CODE = 1;
    private final static Integer ERROR_CODE = 0;
    private final static String SUCCESS_MESSAGE = "success";
    private final static String ERROR_MESSAGE = "error";

    public static <T> Result<T> success() {
        return new Result<>(SUCCESS_CODE, SUCCESS_MESSAGE);
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(SUCCESS_CODE, data);
    }

    public static <T> Result<T> success(Integer code, T data) {
        return new Result<>(code, data);
    }

    public static <T> Result<T> success(Integer code, String message) {
        return new Result<>(code, message);
    }

    public static <T> Result<T> success(Integer code, T data, String message) {
        return new Result<>(code, data, message);
    }

    public static  <T> Result<T>  error() {
        return new Result<>(ERROR_CODE, ERROR_MESSAGE);
    }

    public static  <T> Result<T>  error(String message) {
        return new Result<>(ERROR_CODE, message);
    }

    public static <T> Result<T> error(Integer code, T data) {
        return new Result<>(code, data);
    }

    public static  <T> Result<T>  error(Integer code, String message) {
        return new Result<>(code, message);
    }

    public static <T> Result<T> error(Integer code, T data, String message) {
        return new Result<>(code, data, message);
    }

    public Result<T> add(String key, Object value) {
        this.map.put(key, value);
        return this;
    }
}
